package com.wry.spring.extension;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.LogRecord;

/**
 * <h1>  </h1>
 *
 * @author wry
 * @since 2025.04.06
 */
public class MyFilterLogin implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("MyFilterLogin -> 验证是否登录");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String auth = request.getHeader("auth");
        if (auth.equals("DOASDJASJFASKNKAS12A1D2AS1D")) {
            System.out.println("MyFilterLogin -> 已登录！");
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            System.out.println("MyFilterLogin -> 未登录或已过期！");
            HttpServletResponse response = (HttpServletResponse) servletResponse;
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // 401
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write("{\"message\":\"未登录或登录已过期，请重新登录！\"}");
        }
    }
}
